﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using Frame.Lib.Core.Module;
using Frame.Lib.Common;
using Frame.Lib.Enums;
using Frame.Lib.Utilities;

public partial class Frame_Default : PageBase
{
    private ModuleBO module
    {
        get
        {
            return ModuleBO.Instance<ModuleBO>();
        }
    }

    private string RequestModuleCode
    {
        get
        {
            return Request["ModuleCode"];
        }
    }

    private ModuleInfo firstModule; //一级模块第一个选中的模块

    private string _leftInitModuleID;//左边初始化时选中的模块

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = 0;
        if (!IsPostBack)
        {
            BindList();
            InitJS();
        }
        this.ClientScript.RegisterHiddenField("hidAppPath", GlobalVar.WebAppPath);
        //this.Title = "协同办公平台";
        
        lblUserName.Text = AppSession.UserName;
        lnkLogout.NavigateUrl = GlobalVar.WebAppPath + "logout.aspx";
        AddAddr();
    }

    private void AddAddr()
    {   
        body.Attributes.Add("onload", "resize()");
        //body.Attributes.Add(" onselectstart", "window.event.returnValue=false");
        object moduleID = module.GetIDByCode(ModuleCode.ModifyPwd);
        if (moduleID != null)
        {
            ModuleInfo tmp = module.GetInfo(moduleID);
            int upperID = StrUtil.SplitToInt(tmp.Path)[1];
            lnkModifyPwd.NavigateUrl = "javascript:navModule1(" + upperID + ",''," + tmp.ID + ");";
        }

        moduleID = module.GetIDByCode(ModuleCode.PersonalInfo);
        if (moduleID != null)
        {
            ModuleInfo tmp = module.GetInfo(moduleID);
            int upperID = StrUtil.SplitToInt(tmp.Path)[1];
            lnkPersonalInfo.NavigateUrl = "javascript:navModule1(" + upperID + ",''," + tmp.ID + ");";
        }

        //JSUtil.ExcuteJS("refresh()", "refresh");
    }

    private void BindList()
    {
        int rootID = (int)module.GetRootID();
        int userID = AppSession.UserID;
        List<ModuleInfo> list = module.GetList(rootID, userID).Where(T=>T.State == (int)ModuleState.Enable) .OrderBy(x => x.Orders).ToList();

        rptModule.DataSource = list;
        rptModule.DataBind();

        if (list.Count > 0)
        {
            firstModule = list.First();

            if (!string.IsNullOrEmpty(RequestModuleCode))//如果外部有模块编号传过来
            {
                object requestID = module.GetIDByCode(RequestModuleCode);//这个编号的模块一定要存在
                if (requestID != null)
                {
                    ModuleInfo requestInfo = module.GetInfo(requestID);
                    if (requestInfo.Depth == 2)
                    {
                        firstModule = requestInfo.UpperModuleInfo;
                    }
                    else if (requestInfo.Depth == 3)
                    {
                        firstModule = requestInfo.UpperModuleInfo.UpperModuleInfo;
                    }

                    _leftInitModuleID = requestInfo.ID.ToString();
                }
            }
        }

        if (list.Count > 0)
        {

        }

        //if (dv.Count > 0)
        //{
        //    strLeftFirstUrl = dv[0]["MainUrl"].ToString();
        //    FirstUpperID = dv[0]["ID"].ToString();

        //    //如果外面有模块编号传进来，那就要找到该模块对应的一级模块的
        //    if (strFirstModulePath != null)
        //    {
        //        foreach (DataRowView row in dv)
        //        {
        //            int id = (int)row["ID"];
        //            if (strFirstModulePath.IndexOf("," + id + ",") >= 0)
        //            {
        //                strLeftFirstUrl = row["MainUrl"].ToString();
        //                FirstUpperID = row["ID"].ToString();
        //                break;
        //            }
        //        }
        //    }

        //    if (strLeftFirstUrl == "" || strLeftFirstUrl == "#")
        //    {
        //        strLeftFirstUrl = "Left.aspx?UpperID=" + FirstUpperID + "&ModuleID=" + firstModuleID;
        //    }
        //}

        //repModule.DataSource = dv;
        //repModule.DataBind();
    }

    private void InitJS()
    {
        if (firstModule != null)
        {
            string script = null;
            if (string.IsNullOrEmpty(_leftInitModuleID))
            {
                script = "window.setTimeout(\"navModule1('" + firstModule.ID + "','" + firstModule.NavUrl + "')\",200);";
            }
            else
            {
                script = "window.setTimeout(\"navModule1('" + firstModule.ID + "','" + firstModule.NavUrl + "'," + _leftInitModuleID + ")\",200);";
            }

            this.ClientScript.RegisterStartupScript(this.GetType(), "init", script, true);
        }
    }
    
}
